var $ = require("../../utils/Ajax.js");
Page({
  data: {
    BankId: null,
    BankName: null,
    BankNum: null,
    Card: null,
    UserInfor: null,
    // 底部所有目录控制
    showShopPopup: false,
    animationData: {},

    Money: null,
    Money_: null
  },

  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "提现"
    }) 
    $.Ajax("api/userCard","GET",{},function(res){
      if(res.s){
        that.setData({
          BankId: res.d.userCard[0].id,
          BankName: res.d.userCard[0].bank,
          BankNum: res.d.userCard[0].shortNum,
          Card: res.d.userCard
        })
      }
    })
  },

  onShow: function () {
    var UserInfor = wx.getStorageSync('UserInfor');
    this.setData({
      UserInfor: UserInfor
    })
  },

  // 点击选择银行卡
  SelectBank: function(){
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation;
    animation.translateY(750).step();
    this.setData({
      animationData: animation.export(),
      showShopPopup: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }, 200)
  },

  // 点击选择银行卡
  BankOk: function(e){
    var Bank = e.detail.value.split('_');
    this.setData({
      BankId: Bank[0],
      BankName: Bank[1],
      BankNum: Bank[2]
    })
    this.CashBgClose();
  },

  // 点击关闭选择银行卡
  CashBgClose: function (e) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation;
    animation.translateY(750).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
  },

  // 点击添加银行卡
  AddBank: function(){
    wx.navigateTo({
      url: '../AddBank/AddBank',
    })
  },

  // 输入提现金额
  Money: function(e){
    var Money = e.detail.value;
    var Money_ = this.data.Money_;
    var UserInfor = this.data.UserInfor;
    if (UserInfor.income >= Money){
      this.setData({
        Money_: Money
      })
    }else{
      this.setData({
        Money_: Money_
      })
    }
  },

  // 点击全部提现
  AllMoney: function(){
    var UserInfor = this.data.UserInfor;
    this.setData({
      Money_: UserInfor.income
    })
  },

  // 点击提现
  Withdraw: function(){
    var that = this;
    var Money_ = that.data.Money_;
    if (Money_ >= 10){
      $.Ajax("api/cash", "POST", {
        amount: that.data.Money_,
        cardId: that.data.BankId
      }, function (res) {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500
        })
        if (res.s) {
          var UserInfor = wx.getStorageSync('UserInfor');
          UserInfor.income = UserInfor.income - that.data.Money_;
          wx.setStorageSync("UserInfor", UserInfor);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      })
    }else{
      wx.showToast({
        title: '提现金额不能小于10元',
        icon: 'none',
        duration: 1500
      })
    }
  }
})